package extension

import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import libcore.scope.ActionScope

fun actionScope(
    dispatcher: CoroutineDispatcher = Dispatchers.Main.immediate,
    block: suspend CoroutineScope.() -> Unit
) = ActionScope(dispatcher = dispatcher).launch(block)


/**
 * 与生命周期相关的作用域
 * 默认在LifecycleOwner的onDestroy调用时自动取消协程
 */
fun LifecycleOwner.actionScopeLife(
    dispatcher: CoroutineDispatcher = Dispatchers.Main.immediate,
    block: suspend CoroutineScope.() -> Unit
) = ActionScope(this, dispatcher).launch(block)